package com.ziyucoding.springboot.labs.lab4.event.service.impl;

import com.ziyucoding.springboot.labs.lab4.event.events.MsgEvent;
import com.ziyucoding.springboot.labs.lab4.event.events.OrderProductEvent;
import com.ziyucoding.springboot.labs.lab4.event.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
 * 订单服务实现类
 *
 * @author ziyucoding
 * @date 2022-10-25
 **/
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public String buyOrder(String orderId) {
        long start = System.currentTimeMillis();
        // 1.查询订单详情

        // 2.检验订单价格 （同步处理）
        applicationContext.publishEvent(new OrderProductEvent(this, orderId));

        // 3.短信通知（异步处理）
        applicationContext.publishEvent(new MsgEvent(orderId));

        long end = System.currentTimeMillis();
        log.info("任务全部完成，总耗时：({})毫秒", end - start);
        return "购买成功";
    }
}
